package cn.ggsaas.user.service;

import cn.ggsaas.user.api.SysRoleService;
import cn.ggsaas.user.api.SysUserService;
import cn.ggsaas.user.dto.UserInfo;
import cn.ggsaas.user.mapper.SysUserMapper;
import cn.ggsaas.user.pojo.SysRole;
import cn.ggsaas.user.pojo.SysUser;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 作业系统的用户表 服务实现类
 * </p>
 *
 * @author ggsaas-MpGenerator
 * @since 2020-09-01
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Reference
    private SysRoleService roleService;

    @Override
    public UserInfo getUserInfo(SysUser sysUser) {
        UserInfo userInfo = new UserInfo();
        userInfo.setSysUser(sysUser);
        // 设置角色列表 （ID）
        List<SysRole> roles = roleService.findRolesByUserId(sysUser.getUserId());
        List<Long> roleIds = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
        userInfo.setRoles(ArrayUtil.toArray(roleIds, Long.class));

        return userInfo;
    }
}
